﻿namespace Sage.Http.Models
{
    /// <summary>
    /// 表示上传文件的参数类
    /// </summary>
    /// <param name="content">文件内容流</param>
    /// <param name="fileName">文件名</param>
    /// <param name="formFieldName">表单字段名</param>
    /// <param name="contentType">内容类型</param>
    public class FileParameter(Stream content, string fileName, string? formFieldName = null, string contentType = "application/octet-stream") : IDisposable, IAsyncDisposable
    {
        /// <summary>
        /// 文件内容流
        /// </summary>
        public Stream Content { get; } = content ?? throw new ArgumentNullException(nameof(content));

        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; } = fileName ?? throw new ArgumentNullException(nameof(fileName));

        /// <summary>
        /// 内容类型MIME
        /// </summary>
        public string ContentType { get; } = contentType;

        /// <summary>
        /// 表单字段名
        /// </summary>
        public string? FormFieldName { get; } = formFieldName;

        /// <summary>
        /// 从文件路径创建FileParameter实例
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="formFieldName">表单字段名</param>
        /// <param name="contentType">内容类型</param>
        /// <returns>FileParameter实例</returns>
        public static FileParameter FromFile(string filePath, string? formFieldName = null, string contentType = "application/octet-stream")
        {
            if (string.IsNullOrWhiteSpace(filePath))
                throw new ArgumentNullException(nameof(filePath));
            if (!File.Exists(filePath))
                throw new FileNotFoundException("文件未找到。", filePath);

            var content = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            var fileName = Path.GetFileName(filePath);
            return new FileParameter(content, fileName, formFieldName, contentType);
        }

        /// <summary>
        /// 从字节数组创建FileParameter实例
        /// </summary>
        /// <param name="contentBytes">文件内容字节数组</param>
        /// <param name="fileName">文件名</param>
        /// <param name="formFieldName">表单字段名</param>
        /// <param name="contentType">内容类型</param>
        /// <returns>FileParameter实例</returns>
        public static FileParameter FromBytes(byte[] contentBytes, string fileName, string? formFieldName = null, string contentType = "application/octet-stream")
        {
            if (contentBytes == null || contentBytes.Length == 0)
                throw new ArgumentNullException(nameof(contentBytes));

            var content = new MemoryStream(contentBytes);
            return new FileParameter(content, fileName, formFieldName, contentType);
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 添加异步释放实现
        /// </summary>
        /// <returns></returns>
        public async ValueTask DisposeAsync()
        {
            // 如果内容流支持异步释放，使用异步方式
            if (Content is IAsyncDisposable asyncDisposable)
                await asyncDisposable.DisposeAsync().ConfigureAwait(false);
            else
                Content?.Dispose();

            GC.SuppressFinalize(this);
        }
        /// <summary>
        /// 受保护的虚拟Dispose方法
        /// </summary>
        /// <param name="disposing">是否正在释放托管资源</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // 释放托管资源
                Content?.Dispose();
            }
        }
    }
}